package com.lala.rennigou.controller;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageInfo;
import com.lala.rennigou.pojo.EasybuyOrder;
import com.lala.rennigou.pojo.EasybuyUser;
import com.lala.rennigou.service.EasybuyOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
public class EasybuyOrderController {
    @Autowired
    private EasybuyOrderService easybuyOrderService;
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;
    public EasybuyUser yanzheng(HttpServletRequest request){
        EasybuyUser easybuyUser=null;
        Cookie[] cookies =  request.getCookies();//获取保存在request的所有cookie
        if(cookies != null){//判断cookies数组是否为空
            for(Cookie cookie : cookies){
                if(cookie.getName().equals("user")){//通过for循环找到想要获取的值
                    Map<String ,Object> map=(Map<String ,Object>) redisTemplate.opsForValue().get(cookie.getValue());
                    if(map!=null){
                        easybuyUser=(EasybuyUser)map.get("user");
                    }
                }
            }
        }
        return easybuyUser;
    }


    @RequestMapping("/myOrder")
    public Object myOrder(int userid,int p){
        PageInfo<EasybuyOrder> pageInfo = easybuyOrderService.myOrder(userid,p);
        if(pageInfo.getList()==null){
            return JSONArray.toJSONString("false");
        }
        return JSONArray.toJSON(pageInfo);
    }

    @RequestMapping("/allOrder")
    public Object allOrder(int p){
        PageInfo<EasybuyOrder> pageInfo = easybuyOrderService.allOrder(p);
        if(pageInfo.getList()==null){
            return JSONArray.toJSONString("false");
        }
        return JSONArray.toJSON(pageInfo);
    }

//    @RequestMapping("/addOrder")
//    public Object addOrder(String address,String prices, HttpServletRequest request){
//        EasybuyUser easybuyUser=yanzheng(request);
//
//        EasybuyOrder e=new EasybuyOrder();
//        e.setUseraddress(address);
//        e.setUserid(easybuyUser.getId());
//        e.setLoginname(easybuyUser.getLoginname());
//        e.setCost(Float.parseFloat(prices));
//        e.setSerialnumber(String.valueOf(UUID.randomUUID()));
//        e.setCreatetime(new Date());
//        int i = easybuyOrderService
//                .addOrder(e);
//        if (i>0)return JSONArray.toJSON(true);
//        return JSONArray.toJSON(false);
//    }


    @RequestMapping("/xianshiDD")
    private Object xianshiDD(int userid){
        List<EasybuyOrder> easybuyOrders = easybuyOrderService.selectByID(userid);
        return JSONArray.toJSON(easybuyOrders);
    }

    //退款
    @RequestMapping("/tuikuan")
    private Object tuikuan(int id){
        EasybuyOrder easybuyOrder = easybuyOrderService.selectById(id);
        Date date=new Date();
        long time=date.getTime();
        if ((time-easybuyOrder.getCreatetime().getTime())/1000/60>30){
            return JSONArray.toJSONString(false);
        }
        return JSONArray.toJSONString(true);
    }
}
